package restfuljobs

import static org.grails.jaxrs.response.Responses.*

import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.Produces
import javax.ws.rs.PUT
import javax.ws.rs.core.Response
import javax.ws.rs.QueryParam

import org.grails.jaxrs.provider.DomainObjectNotFoundException

@Consumes(['application/xml','application/json'])
@Produces(['application/xml','application/json'])
class JobApplicationResource {
    
    def jobApplicationResourceService
    def id
    
    @GET
    Response read(@QueryParam("userId") Long userId) {
		
		if (!userId) {
			throw new AccessDeniedException(JobApplicationReview.class, userId)
		}
		
		def user = User.get(userId)
		def application = jobApplicationResourceService.read(id)
		
		if((application.applicant.id != userId) && (application.jobListing.recruiter.id != userId)
			&&(user.type != User.Type.REVIEWER)) {
			throw new AccessDeniedException(JobApplicationReview.class, userId)
		}
		
        ok application
    }
    
    @PUT
    Response update(JobApplication dto, @QueryParam("userId") Long userId) {
        dto.id = id
        ok jobApplicationResourceService.update(dto, userId)
    }
    
    @DELETE
    void delete() {
        jobApplicationResourceService.delete(id)
    }
    
}